
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       mb_socket.h
  * @author     baiyang
  * @date       2022-12-1
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <rtconfig.h>

#if defined(RT_USING_SAL)
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
typedef struct mb_socket* mb_socket_t;
typedef const struct mb_socket* mb_socket_const_t;

struct mb_socket {
    struct sockaddr_in in_addr;
    int fd;
    bool datagram;
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void mb_socket_ctor(mb_socket_t s, bool _datagram);
void mb_socket_ctor2(mb_socket_t s, bool _datagram, int _fd);
void mb_socket_make_sockaddr(const char *address, uint16_t port, struct sockaddr_in *sockaddr);
bool mb_socket_connect(mb_socket_t s, const char *address, uint16_t port);
void mb_socket_close(mb_socket_t s);
bool mb_socket_bind(mb_socket_t s, const char *address, uint16_t port);
bool mb_socket_reuseaddress(mb_socket_const_t s);
bool mb_socket_set_blocking(mb_socket_const_t s, bool blocking);
bool mb_socket_set_cloexec(mb_socket_const_t s);
static inline ssize_t mb_socket_send(mb_socket_const_t s, const void *buf, size_t size) { return send(s->fd, buf, size, 0); }
ssize_t mb_socket_sendto(mb_socket_t s, const void *buf, size_t size, const char *address, uint16_t port);
static inline ssize_t mb_socket_sendto2(mb_socket_t s, const void *buf, size_t size, struct sockaddr_in *sockaddr_i) { return sendto(s->fd, buf, size, 0, (struct sockaddr *)sockaddr_i, sizeof(struct sockaddr_in)); }
ssize_t mb_socket_recv(mb_socket_t s, void *buf, size_t size, uint32_t timeout_ms);
void mb_socket_last_recv_address(mb_socket_const_t s, const char **ip_addr, uint16_t *port);
void mb_socket_set_broadcast(mb_socket_const_t s);
bool mb_socket_pollin(mb_socket_t s, uint32_t timeout_ms);
bool mb_socket_pollout(mb_socket_t s, uint32_t timeout_ms);
bool mb_socket_listen(mb_socket_const_t s, uint16_t backlog);

/*------------------------------------test------------------------------------*/
#endif

#ifdef __cplusplus
}
#endif



